<template>
	<view class="new_linkman">
		<view class="fllow_form">
			<view><text>*</text>客户名称</view>
			<picker @change="changeCus" :value="cusIndex" :range="cuslist">
				<view class="form_right">
					<view :class="customerName ? 'valueActive' : '' ">{{customerName ? customerName : '点击选择'}}</view>
					<view class="iconfont icon-arrows_right"></view>
				</view>
			</picker>
		</view>
		<view class="fllow_form">
			<view><text>*</text>客户签约人</view>
			<picker @change="changeContact" mode="selector" :range="contactList" :value="contactIndex"
				:range-key="'name'">
				<view class="form_right">
					<view :class="contactList[contactIndex] ? 'valueActive' : '' ">
						{{contactList[contactIndex] ? contactList[contactIndex].name : '点击选择'}}</view>
					<view class="iconfont icon-arrows_right"></view>
				</view>
			</picker>
		</view>
		<view class="fllow_form">
			<view><text>*</text>公司签约人</view>
			<picker @change="changeStaff" :range-key="'name'" :range="staffList" :value="staffIndex">
				<view class="form_right">
					<view :class="staffList[staffIndex] ? 'valueActive' : '' ">
						{{staffList[staffIndex] ? staffList[staffIndex].name : '点击选择'}}</view>
					<view class="iconfont icon-arrows_right"></view>
				</view>
			</picker>
		</view>
		<form-item v-for="(item,index) in listForm" :key="index" :objInfo="item" @changeData="changeOtherData">
		</form-item>

		<!-- 审核信息 -->
		<view class="info_head">审核信息</view>
		<view class="affiliation">
			<view class="aff_head"><text>*</text>由谁审核</view>
			<view class="aff_con">
				<view class="aff_item affbox" v-for="(item,index) in remindPeople" :key="index">
					<image :src="BASE_IMG_URL+'close.png'" class="delremind" v-if="item.isDel" @click="delRemind(index)"
						 mode="scaleToFill"></image>
					<image :src="item.img ? item.img :BASE_IMG_URL+'headImg.png'" mode="scaleToFill"></image>
					<view>{{item.name}}</view>
				</view>
				<view class="aff_last" @click="moreClick">
					<image :src="BASE_IMG_URL+'morebtn.png'" mode="scaleToFill"></image>
				</view>
			</view>
		</view>

		<view id="btn" class="btn_g bg-blue button-hover round" @click="$noMultipleClicks(toSave)">
			保存
		</view>
	</view>
</template>

<script>
	import { BASE_IMG_URL } from '@/util/api.js'
	import {
		netSetForm,
		netStaffDefaultReviewer
	} from '@/api/index.js'
	import formItem from '@/components/form/formitem.vue'
	import {
		netPrevCustomer,
		netSelect,
		netLianxiren,
		netStaffList,
		netCreateContract
	} from '@/api/kehu.js'
	import {
		dateTimePicker,
		getMonthDay
	} from '@/util/dateTimePicker.js'
	export default {
		components: {
			formItem
		},
		data() {
			return {
				noClick: true, //防止  重复点击
				name: '', //合同名称
				num: '', //合同编号
				order_date: '', //下单时间
				money: '', //合同金额

				start_time: '', //开始时间
				end_time: '', //结束时间
				order_staff_id: '', //公司签约人
				staffList: [],
				staffIndex: null,

				customer_id: '', //客户id
				contacts_id: '', //客户签约人
				contactList: [],
				contactName: '',
				contactIndex: null,

				customerlist: [],
				customerName: '', //客户名称
				cuslist: [],
				cusIndex: null,

				dateTime: null,
				dateTimeArray: null,
				dateArr: null,
				startYear: 2000,
				BASE_IMG_URL:BASE_IMG_URL,
				DefaultRemind: [],
				remindPeople: [], //提醒谁看
				listForm: [],
			}
		},
		onLoad(options) {
			this.customer_id = options.id
			//获取客户 列表
			this.getCustomerList()
			//获取  员工列表  公司签约人
			this.getStaffList()
			this.listForm = []
			this.getForm()
		},
		onShow() {
			//获取  默认需要提醒的人
			this.getDefaultRemind()
		},
		//监听  页面 卸载
		onUnload() {
			uni.removeStorageSync('remindlist')
		},
		methods: {
			//获取  需要默认提醒的人
			getDefaultRemind() {
				netStaffDefaultReviewer({
					type: 'contract'
				}).then(res => {
					res = res.data
					res.forEach((ele, index) => {
						ele.isDel = false
					})
					this.DefaultRemind = res
					this.remindPeople = this.$deWeight([...res, ...uni.getStorageSync('remindlist')])
				})
			},
			getForm() {
				netSetForm({
					type: 'contract'
				}).then(res => {
					res = res.data.data
					res.forEach(ele=>{
						ele.value = ''
					})
					this.listForm = res
				})
			},
			changeOtherData(name, value, obj) {
				let arr = this.listForm
				arr.forEach((ele, index) => {
					if (ele.id == obj.id) {
						ele.value = value
						this.$set(this.listForm, index, ele)
					}
				})
			},
			//删除 提醒人
			delRemind(index) {
				this.remindPeople.splice(index, 1)
				uni.setStorageSync('remindlist', this.remindPeople)
			},
			//员工列表
			getStaffList() {
				netStaffList().then(res => {
					this.staffList = res.data
				})
			},
			getCustomerList() {
				netPrevCustomer().then(res => {
					this.customerlist = res.data
					this.handleCus(res.data)
				})
			},
			handleCus(data) {
				let arr = []
				data.forEach(ele => {
					arr.push(ele.name)
				})
				this.cuslist = arr
				this.setDefaultKehu()
			},
			setDefaultKehu() {
				let id = this.customer_id
				this.customerlist.forEach((ele, index) => {
					if (ele.id == id) {
						this.customerName = ele.name
						this.cusIndex = index
					}
				})
				this.getLianxiren()
			},
			changeCus(e) {
				this.cusIndex = e.detail.value
				this.customerName = this.customerlist[e.detail.value].name
				this.customer_id = this.customerlist[e.detail.value].id
				this.getLianxiren()
			},
			getLianxiren() {
				let params = {
					customer_id: this.customer_id
				}
				netLianxiren(params).then(res => {
					if (res.data.length == 0) {
						uni.showToast({
							title: '请先添加联系人',
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 2000)
						return
					}
					this.contactList = res.data
				})
			},
			//客户签约人
			changeContact(e) {
				this.contactIndex = e.detail.value
				this.contacts_id = this.contactList[e.detail.value].id
				console.log(this.contacts_id,'客户签约人')
			},
			//公司签约人
			changeStaff(e) {
				this.staffIndex = e.detail.value
				this.order_staff_id = this.staffList[e.detail.value].id
				console.log(this.contacts_id,'公司签约人')
				console.log(this.staffIndex,'公司签约人')
			},
			moreClick() {
				uni.navigateTo({
					url: '/pagesA/crm/selectMember/selectMember'
				})
			},
			//保存
			toSave() {
				let {
					order_staff_id, //公司签约人
					customer_id, //客户id
					contacts_id, //客户签约人
					remindPeople, //提醒谁看
				} = this
				if (!contacts_id) {
					uni.showToast({
						title: '请选择客户签约人',
						icon: 'none'
					})
					return
				}
				if (!order_staff_id) {
					uni.showToast({
						title: '请选择公司签约人',
						icon: 'none'
					})
					return
				}
				let arrlist = []
				let params = {}
				this.listForm.forEach(ele => {
					arrlist.push({
						name: ele.id,
						required: ele.config.required,
						msg: ele.config.placeholder,
						value: ele.value
					})
				})
				for (let i = 0; i < arrlist.length; i++) {
					if (arrlist[i].required && !arrlist[i].value) {
						uni.showToast({
							title: arrlist[i].msg,
							icon: 'none'
						})
						return
					}
					params[arrlist[i].name] = arrlist[i].value
				}

				if (remindPeople.length == 0) {
					uni.showToast({
						title: '请选择审核人',
						icon: 'none'
					})
					return
				}
				let arr = []
				remindPeople.forEach(ele => {
					arr.push(ele.id)
				})
				let flow_staff_id = arr.join(',')
				params = Object.assign({}, params, {
					order_staff_id,
					customer_id,
					contacts_id,
					flow_staff_ids:flow_staff_id
				})
				netCreateContract(params).then(res => {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 2000
					})
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 2000)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.valueActive {
		color: #333;
	}

	.new_linkman {
		font-size: 28rpx;
		.mar_top {
			margin-top: 30rpx;
		}
		.info_head {
			height: 80rpx;
			line-height: 80rpx;
			color: #999;
		    font-size: 32rpx;
			padding-left: 30rpx;
		}
		.fllow_form {
			display: flex;
			justify-content: space-between;
			background-color: #fff;
			border-bottom: 1rpx solid #EAEAEA;
			padding: 30rpx 20rpx;
			color: #999;
			font-size: 32rpx;
			.form_right {
				display: flex;
				align-items: center;
				color: #999;
				:first-child {
					width: 450rpx;
					text-align: right;
				}
				image {
					width: 40rpx;
					height: 40rpx;
					margin: 28rpx 5rpx 0 0;
				}
			}
			.theme {
				color: #5ca9fe;
			}
			text {
				color: #f00;
				margin-left: 10rpx;
			}
			.form_input {
				input {
					height: 100rpx;
					line-height: 100rpx;
					text-align: right;
					margin-right: 10rpx;
				}
			}
		}

		.fllow_area {
			background-color: #fff;
			padding: 0 30rpx;
			margin-bottom: 30rpx;

			.area_head {
				height: 100rpx;
				line-height: 100rpx;

				text {
					color: #f00;
				}
			}

			textarea {
				width: 100%;
				height: 300rpx;
				background: #f8f8f8;
			}
		}
	}

	.affiliation {
		background-color: #fff;
		margin-bottom: 20rpx;
		padding-bottom: 40rpx;

		.aff_head {
			height: 100rpx;
			line-height: 100rpx;
			padding-left: 20rpx;
			color: #999;
			font-size: 32rpx;
			text {
				color: #f00;
				margin-left: 10rpx;
			}
		}

		.aff_con {
			display: flex;
			text-align: center;
			flex-wrap: wrap;
			padding:0 35rpx;
			.aff_item {
				width: 100rpx;
				margin-right: 15rpx;
				margin-bottom: 15rpx;

				image {
					margin-bottom: 15rpx;
					width: 60rpx;
					height: 60rpx;
					border-radius: 50%;
				}
			}

			.affbox {
				position: relative;

				.delremind {
					width: 32rpx;
					height: 32rpx;
					border-radius: 50%;
					position: absolute;
					right: -16rpx;
					top: -16rpx;
					background: rgba(0, 0, 0, 0.5);
					z-index: 2;
				}
			}

			.aff_last {
				width: 48rpx;
				height: 48rpx;
				border-radius: 50%;
				text-align: center;
				image {
					width: 48rpx;
					height: 48rpx;
				}
			}
		}
	}

	#btn {
		font-size: 26rpx;
		color: #fff;
		text-align: center;
		line-height: 88rpx;
		width: 500rpx;
		height: 88rpx;
		background: #03AA96;
		border-radius: 29rpx;
	}
</style>
